package com.ts.animal.controller;

import com.ts.animal.domain.SubstandardDataDO;
import com.ts.animal.service.SubstandardDataService;
import com.ts.common.annotation.Log;
import com.ts.common.utils.PageInfos;
import com.ts.common.utils.PageUtils;
import com.ts.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 耗损数据表
 *
 * @author bobby
 * @email bobby@126.com
 * @date 2020-02-04 22:04:36
 */

@Controller
@RequestMapping("/animal/substandardData")
public class SubstandardDataController {
    @Autowired
    private SubstandardDataService substandardDataService;

    @GetMapping()
    @RequiresPermissions("animal:substandardData:substandardData")
    String SubstandardData() {
        return "animal/substandardData/substandardData";
    }

    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("animal:substandardData:substandardData")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        PageUtils pageUtils = new PageInfos(params) {
            public List inject(Map params) {
                return substandardDataService.list(params);
            }
        }.query();
        return pageUtils;
    }


    /**
     * 审核次品
     */
    @Log("审核动物耗损申请单")
    @ResponseBody
    @PostMapping("/approval")
    @RequiresPermissions("animal:substandardData:approval")
    public Result approval(@RequestParam("ids[]") Integer[] ids) {
        if (substandardDataService.approval(ids) > 0) {
            return Result.ok();
        }
        return Result.error();
    }


    @GetMapping("/add")
    @RequiresPermissions("animal:substandardData:add")
    String add() {
        return "animal/substandardData/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("animal:substandardData:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        SubstandardDataDO substandardData = substandardDataService.get(id);
        model.addAttribute("substandardData", substandardData);
        return "animal/substandardData/edit";
    }


    /**
     * 加入耗损
     */
    @Log("提交动物耗损申请单")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("animal:substandardData:add")
    public Result save(SubstandardDataDO substandardData) {
        if (substandardDataService.save(null, substandardData) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 修改
     */
    @Log("修改动物耗损申请单")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("animal:substandardData:edit")
    public Result update(SubstandardDataDO substandardData) {
        substandardDataService.update(null, substandardData);
        return Result.ok();
    }

    /**
     * 删除
     */
    @Log("删除动物耗损申请单")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("animal:substandardData:remove")
    public Result remove(Integer id) {
        if (substandardDataService.remove(id) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 删除
     */
    @Log("批量删除动物耗损申请单")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("animal:substandardData:batchRemove")
    public Result remove(@RequestParam("ids[]") Integer[] ids) {
        substandardDataService.batchRemove(ids);
        return Result.ok();
    }

}
